import { getToken } from "@/utils/auth";
import ArticlePage from "@/views/articlePage.vue";
import HomePage from "@/views/homePage.vue";
import LayoutPage from "@/views/layoutPage.vue";
import LoginPage from "@/views/loginPage.vue";
import PublishPage from "@/views/publishPage.vue";
import UserView from "@/views/userView.vue";
import { ElMessage } from "element-plus";
import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: "/login", component: LoginPage },
    { path: '/', redirect: '/home' ,component: LayoutPage,
      children:[
        { path: 'home', component: HomePage },
        { path: 'article', component: ArticlePage },
        { path: 'publish', component: PublishPage },
        {path:'/user',component: UserView}
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  const token = getToken()
  if (!token && to.path !== '/login') {
    ElMessage.error('登录信息无效,请先登录')
    next('/login')
  }
  next()
})

export default router
